package com.logic.landseaserver.ws;

import java.util.List;

import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.logic.common.enums.HttpCodeEnum;
import com.logic.common.util.CommonResult;
import com.logic.common.util.CommonResultMap;
import com.logic.common.util.QueryUtil;
import com.logic.landseaserver.common.exception.LandseaBusinessException;
import com.logic.landseaserver.domain.CleanLog;
import com.logic.landseaserver.domain.OperationLog;
import com.logic.landseaserver.service.IOperationLogService;
import com.logic.landseaserver.ws.dto.CleanLogDTO;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * @Author: jack
 * @Date: 2017/5/26
 * @Description: 账单支付控制类
 */
@Path("/operationLog")
@Api(value = "操作日志")
public class OperationLogRest {
    private static final Logger LOGGER = LoggerFactory.getLogger(OperationLogRest.class);

    @Autowired
    private IOperationLogService operationLogService;

    @POST
    @Path("/list")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "日志查询", position = 1)
    public Response createBillPayment(@ApiParam(value = "param", required = true) QueryUtil queryUtil) {
        CommonResultMap map = new CommonResultMap();
        try {
            map.setData(operationLogService.query(queryUtil));
            map.setResult(CommonResult.SUCCESS, "Create success");
        } catch (Exception e) {
            map.setResult(CommonResult.ERROR, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }

    @POST
    @Produces(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "添加日志", position = 14)
    public Response create(@ApiParam(value = "param", required = true) OperationLog log) {
        CommonResultMap map = new CommonResultMap();
        try {
            operationLogService.insert(log);
            map.setResult(CommonResult.SUCCESS);
        } catch (LandseaBusinessException e) {
            map.setResult(CommonResult.ERROR, e.getMessage());
        } catch (Exception e) {
            LOGGER.error("clearBillPaymentDue error", e);
            map.setResult(CommonResult.ERROR, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }
    
    @POST
    @Path("/cleanLog")
    @Produces(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "清理日志", position = 14)
    public Response cleanLog( ) {
        CommonResultMap map = new CommonResultMap();
        try {
            operationLogService.cleanLog();
            CleanLog log = new CleanLog();
            operationLogService.saveCleanLog(log);
            map.setResult(CommonResult.SUCCESS,"cleanLog success");
        } catch (Exception e) {
            LOGGER.error("cleanLog error", e);
            map.setResult(CommonResult.BUSINESS_EXCEPTION, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }
   
    //查出日志
    @POST
    @Path("/queryCleanLog")
    @Produces(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "查询日志", position = 14)
    public Response queryCleanLog() {
        CommonResultMap map = new CommonResultMap();
        try {
           List<CleanLogDTO> cleanLogDTOs =  operationLogService.selectCleanLog();
           map.setData(cleanLogDTOs);
            map.setResult(CommonResult.SUCCESS);
        } catch (Exception e) {
            LOGGER.error("clearBillPaymentDue error", e);
            map.setResult(CommonResult.ERROR, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }
}
